from django.db import models
import django.utils.timezone as timezone
# from django.db.backends.mysql.base import DatabaseFeatures
# DatabaseFeatures.supports_microsecond_precision = False
# Create your models here.
# === 图纸管理系统model
# 传感器分类 -- sensor ,项目 -- project , 图纸信息 -- drawing

# 公有创建时间和修改时间

class TimeStampMode(models.Model):
  # created = models.DateTimeField(default=timezone)
  created = models.DateTimeField(auto_now_add=True)
  modified = models.DateTimeField(auto_now=True)

  class Meta:
    abstract = True

# 传感器类 --- sensor: Id,name,created,modified
class Sensor(TimeStampMode):
  sensor_name = models.CharField(verbose_name="传感器名称",max_length=50,unique=True,null=True,blank=False)

  class Meta:
    db_table = "Draw_Sensor"
    managed = True
    app_label = "app01"
  
  def __str__(self):
    return str(self.sensor_name)
  
#  项目类 project: id,name,created,modified
class Project(TimeStampMode):
  project_name = models.CharField(verbose_name="项目名称",max_length=50,unique=True,null=False,blank=False)
  sensor = models.ForeignKey(verbose_name="所属传感器",to=Sensor,on_delete=models.PROTECT)

  class Meta:
    db_table = "Draw_Project"
    managed = True
    app_label = "app01"
  
  def __str__(self):
    return str(self.project_name)
  
# 资料类 data: id,name,created,modified
class Data(TimeStampMode):
  data_name = models.CharField(verbose_name="资料名称",max_length=50,unique=True,blank=False)
  sensor = models.ForeignKey(verbose_name="所属传感器",to=Sensor,on_delete=models.PROTECT)
  project = models.ForeignKey(verbose_name="所属项目",to=Project,on_delete=models.PROTECT)

  class Meta:
    db_table = "Draw_Data"
    managed = True
    app_label = "app01"

  def __str__(self):
    return str(self.data_name)
  
# 图纸信息 drawing:ID,drawing_name,created,modified,drawing_page,drawing_client_id,drawing_remark,drawing_version
class Drawing(TimeStampMode):
  material_code = models.CharField(verbose_name="物料编号", max_length=50, blank=False)  
  sensor = models.ForeignKey(verbose_name="所属传感器", to=Sensor, on_delete=models.PROTECT)
  project = models.ForeignKey(verbose_name="所属项目", to=Project, on_delete=models.PROTECT)
  data = models.ForeignKey(verbose_name="所属资料", to=Data, on_delete=models.PROTECT)
  drawing_name = models.CharField(verbose_name="材料名称", max_length=50, blank=False)
  drawing_spec = models.CharField(verbose_name="规格/图纸号", max_length=50, blank=False)
  drawing_page = models.CharField(verbose_name="图纸页数", max_length=50, blank=False)
  drawing_client_id = models.CharField(verbose_name="客户编号", max_length=50)
  drawing_version = models.CharField(verbose_name="版本号", max_length=50, blank=False)
  drawing_remark = models.CharField(verbose_name="备注", max_length=100)
  drawing_url = models.CharField(verbose_name="地址", max_length=100)
  is_deleted = models.IntegerField(verbose_name="逻辑删除0否1是", blank=False, default=0)

  class Meta:
    db_table="Draw_Drawing"
    managed = True
    app_label = "app01"
  
  def __str__(self):
    return str(self.drawing_name)
